# -*- coding: utf-8 -*-
# @author: HRUN
import json

from celery import shared_task

from performance.models import PerformanceTask
from performance.serializers import PerformanceTaskRunSerializer

def __get_env_config(env_id, debug=False):
    pass

@shared_task
def run_task(task_id, env_id):
    config = __get_env_config(env_id, debug=True)
    # 获取执行任务的数据
    task = PerformanceTask.objects.get(pk=task_id)
    task_data = PerformanceTaskRunSerializer(task).data
    presetting = task_data['presetting']
    scene_list = []
    for scene in task_data['taskScence']:
            steps = scene['taskscencestep_set']
            # 排序
            steps.sort(key=lambda x: x['sort'])
            def process_item(item):
                processed_item = {**item['stepInfo'], **{'children': []}}
                if 'children' in item:
                    processed_item['children'] = [process_item(child) for child in item['children']]
                return processed_item
            scene_list.append({'steps': [process_item(item) for item in steps], 'name': scene['name'],'env': scene['env'],'weight': scene['weight']})
    data = {'scenes': scene_list, 'presetting':{**presetting}}
    print(json.dumps(data))
    return json.dumps(data)